package cn.shengchao.examstar.ai.domain.valueobject;

import lombok.Getter;

import java.util.Objects;

/**
 * 学习时长值对象
 */
@Getter
public class StudyDuration {
    
    private final Long minutes;
    
    private StudyDuration(Long minutes) {
        if (minutes == null || minutes <= 0) {
            throw new IllegalArgumentException("学习时长必须大于0分钟");
        }
        if (minutes > 24 * 60) {
            throw new IllegalArgumentException("每日学习时长不能超过24小时");
        }
        this.minutes = minutes;
    }
    
    /**
     * 创建学习时长（分钟）
     */
    public static StudyDuration ofMinutes(Long minutes) {
        return new StudyDuration(minutes);
    }
    
    /**
     * 创建学习时长（小时）
     */
    public static StudyDuration ofHours(Double hours) {
        if (hours == null || hours <= 0) {
            throw new IllegalArgumentException("学习时长必须大于0小时");
        }
        return new StudyDuration((long) (hours * 60));
    }
    
    /**
     * 获取小时数
     */
    public Double getHours() {
        return minutes / 60.0;
    }
    
    /**
     * 判断是否为合理的学习时长
     */
    public boolean isReasonable() {
        // 合理的学习时长应该在30分钟到8小时之间
        return minutes >= 30 && minutes <= 8 * 60;
    }
    
    /**
     * 增加学习时长
     */
    public StudyDuration add(StudyDuration other) {
        return new StudyDuration(this.minutes + other.minutes);
    }
    
    /**
     * 减少学习时长
     */
    public StudyDuration subtract(StudyDuration other) {
        long result = this.minutes - other.minutes;
        if (result <= 0) {
            throw new IllegalArgumentException("减少后的学习时长必须大于0");
        }
        return new StudyDuration(result);
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StudyDuration that = (StudyDuration) o;
        return Objects.equals(minutes, that.minutes);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(minutes);
    }
    
    @Override
    public String toString() {
        if (minutes < 60) {
            return minutes + "分钟";
        } else {
            double hours = minutes / 60.0;
            return String.format("%.1f小时", hours);
        }
    }
}